﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using StructureMap;
using log4net;

namespace WebApp.Bootstrap.StructureMap
{
    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            try
            {
                if ((requestContext == null) || (controllerType == null))
                    return null;

                return ObjectFactory.GetInstance(controllerType) as IController;
            }
            catch(Exception ex)
            {
                do
                {
                    log.Error(ex.Message);
                    ex = ex.InnerException;
                } while (ex != null);
                    
                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
                return base.GetControllerInstance(requestContext, controllerType);
            }
        }
    }
}